//
//  DsTouxiangVC.m
//  Tour
//
//  Created by 张张烨 on 16/3/9.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "DsTouxiangVC.h"
#import "DsTouxiangView.h"
#import "DsTouxiangYoujiCell.h"
#import "DsTouxiangxihuanCell.h"
#import "DsTouxiangModel.h"
#import "DsCellVC.h"
@interface DsTouxiangVC ()<UITableViewDataSource,UITableViewDelegate,pass11111,UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,LayoutItemHeightDelegate>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *tableArr;
@property (nonatomic, strong) UIScrollView *scrollView;
/**<  以下是collection 的元件 */
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *collData;
@end

@implementation DsTouxiangVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self data];
    [self scrollView1];
    [self tableView1];
    [self collectionDate];
    [self collectionView1];
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)collectionDate
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://chanyouji.com/api/users/likes/%@.json?per_page=18&page=1",self.userid] completion:^(id result) {
        NSLog(@"%@",self.userid);
        NSArray *arr = result;
        self.collData = [DsTouxiangModel modelHanderWithArray:arr];
        [_collectionView reloadData];
        
    }];
}
- (void)collectionView1
{
    Layout *layout = [[Layout alloc]init];
    /**< 列数 */
    layout.columCounts = 2;
    /**< 列间距 */
    layout.columSpace = 5;
    /**< 行间距 */
    layout.rowSpace = 5;
    /**< 边距 */
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.delegate = self;
    
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(WIDTH, 64, WIDTH, HEIGH - 280 * GAO) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];             
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 滚动条隐藏
    _collectionView.showsVerticalScrollIndicator =NO;
    [self.scrollView addSubview:_collectionView];
    //
    [_collectionView registerClass:[DsTouxiangxihuanCell class] forCellWithReuseIdentifier:@"collectioncell"];
}

#pragma mark - 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _collData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DsTouxiangxihuanCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectioncell" forIndexPath:indexPath];
    cell.model = _collData[indexPath.item];
   
    
    return cell;
}

// 设置图片高度
- (CGFloat) layout:(QpLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexpath width:(CGFloat)width
{
    DsTouxiangModel *q = _collData[indexpath.item];
    CGFloat h = width *q.height.doubleValue / q.width.doubleValue;
    return h;
}




/**<  以下是游记的table */
- (void)data
{
    [AFNModel GETWithHandler:[NSString stringWithFormat:@"http://chanyouji.com/api/users/%@.json?page=1",self.userid] completion:^(id result) {
        NSArray *arr = result[@"trips"];
        NSLog(@"%@",[NSString stringWithFormat:@"http://chanyouji.com/api/users/%@.json?page=1",self.userid]);
        self.tableArr =[DsTouxiangModel modelHanderWithArray: arr];
        
        DsTouxiangView *touView = [[DsTouxiangView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 238) mingzi:result[@"name"] touxiang:result[@"image"] xingbie:result[@"gender"]];
        touView.delegate = self;
        [self.view addSubview:touView];
        
        [_tableView reloadData];
    }];
    
}

- (void)scrollView1
{
    // 1.创建
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 110 *GAO, WIDTH, HEIGH - (110 * GAO ))];
    // 2.设置

    // 3.添加父视图
    [self.view addSubview:_scrollView];
    _scrollView.delegate = self;
    /**< contentSize 内容大小 */
    _scrollView.contentSize = CGSizeMake(WIDTH * 2, 0);
    /**< pagingEnbaled 整页翻动 */
    _scrollView.pagingEnabled = YES;

}

- (void)tableView1
{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGH - (181 * GAO)) style:(UITableViewStylePlain)];
    
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    [self.scrollView addSubview:_tableView];
    
    [self.tableView registerClass:[DsTouxiangYoujiCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DsTouxiangYoujiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _tableArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250 * GAO;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DsCellVC  *next = [[DsCellVC alloc]init];
       DsTouxiangModel *m =  _tableArr[indexPath.row];
    next.userid =m.idc;
    [self.navigationController pushViewController:next animated:YES];
}


- (void)xihuan
{
     [_scrollView setContentOffset:CGPointMake(WIDTH , 0) animated:YES];
}

- (void)youji
{
   [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   //  @"https://chanyouji.com/api/users/105194.json?page=1" 游记
    
    // @"https://chanyouji.com/api/users/likes/105194.json?per_page=15&page=1" 瀑布流
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
